package com.yanqu.road.server.pb;

import com.yanqu.road.entity.config.match.MatchInfo;
import com.yanqu.road.entity.config.match.MatchReward;
import com.yanqu.road.entity.match.MatchData;
import com.yanqu.road.entity.match.UserMatchData;
import com.yanqu.road.pb.variety.VarietyProto;
import com.yanqu.road.server.manager.match.CrossMatchConfigMgr;
import com.yanqu.road.server.manager.match.Match;

import java.util.Set;

/**
 * @author ：zfr
 * @date ：2021/2/6 14:49
 */
public class CrossVarietyPb {
    public static VarietyProto.VarietyTempMsg.Builder parseVariety(Match match, long playerId, long serverId) {
        VarietyProto.VarietyTempMsg.Builder builder = VarietyProto.VarietyTempMsg.newBuilder();
        MatchData matchData = match.getMatchData();
        builder.setId(matchData.getId());
        builder.setLevel(matchData.getLevel());
        builder.setOccupation(matchData.getType());
        builder.setVarietyId(matchData.getMatchId());
        MatchInfo matchInfo = CrossMatchConfigMgr.getMatchInfo(matchData.getLevel(), matchData.getMatchId());
        builder.setMaxNum(matchInfo != null ? matchInfo.getUserNumLimit() : 0);
        builder.setCurrentNum(match.getVarietyUserNum());
        builder.setIsJoin(match.isJoin(serverId, playerId));
        builder.setIsGroupChange(matchData.isOverForGroupChange());
        return builder;
    }

    public static VarietyProto.UserDispatchTeamTempMsg.Builder parseTeam(Match match, UserMatchData userMatchData, int userRank) {
        VarietyProto.UserDispatchTeamTempMsg.Builder builder = VarietyProto.UserDispatchTeamTempMsg.newBuilder();
        MatchData matchData = match.getMatchData();
        builder.setId(matchData.getId());
        builder.setLevel(matchData.getLevel());
        builder.setOccupation(matchData.getType());
        builder.addAllPatronsId(userMatchData.getPatronsMap().keySet());
        builder.setTotalEarnSpeed(userMatchData.getTotalEarnSpeed().longValue());
        builder.setVarietyId(matchData.getMatchId());
        if (match.checkIsInRewardTime() || matchData.isOverForGroupChange()) {
            builder.setRank(userRank);
        }
        builder.setTimeId(matchData.getTimeId());
        return builder;
    }

    public static VarietyProto.VarietyMyRank.Builder parseVarietyRank(int timeId, int level, int type, int matchId, int id, Set<Integer> patronsIds, int rank) {
        VarietyProto.VarietyMyRank.Builder builder = VarietyProto.VarietyMyRank.newBuilder();
        builder.setTimeId(timeId);
        builder.setLevel(level);
        builder.setType(type);
        builder.setMatchId(matchId);
        builder.setId(id);
        builder.addAllPatronsId(patronsIds);
        builder.setIndex(rank);
        return builder;
    }
}
